#!/usr/bin/env python
# _*_ coding: utf-8 _*_

# This will first fetch all interfaces of the device and then create rrd database according to interfaces

import rrdtool
import sys
import time
import os

from utils.snmp_query import QueryOidData

ip = '127.0.0.1'
community = 'public'
file_location = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '../static/img')
cur_time = str(int(time.time()))

rra_format = ['RRA:AVERAGE:0.5:1:600',
              'RRA:AVERAGE:0.5:6:700',
              'RRA:AVERAGE:0.5:24:775',
              'RRA:MAX:0.5:1:600',
              'RRA:MAX:0.5:6:700',
              'RRA:MAX:0.5:24:775',
              'RRA:MIN:0.5:1:600',
              'RRA:MIN:0.5:6:700',
              'RRA:MIN:0.5:24:775']


def create_rrddb(path, ds):
    if isinstance(path, list) and isinstance(ds, list):
        rrd_format = path + ['--step', '300', '--start', cur_time] + ds + rra_format
        rrdtool.create(rrd_format)
    else:
        return


def main():
    cpu_path = [os.path.join(file_location, 'cpu_util.rrd')]
    cpu_ds = ['DS:cpu_util:GAUGE:600:0:100']
    create_rrddb(cpu_path, cpu_ds)

    if_ds = ["DS:in:COUNTER:600:0:U", "DS:out:COUNTER:600:0:U"]
    interfaces = QueryOidData(ip, community).ifnames
    for interface in interfaces:
        interface_dbpath = os.path.join(interface + '.rrd', interface + '.rrd')
        create_rrddb(interface_dbpath, if_ds)


if __name__ == '__main__':
    main()

